/**
 * Copyright (c) 2006
 *      OpenAVS Developers. All Rights Reserved.
 *
 * Copyright (c) 2005-2006
 *      NSCC. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * ! \file "global.h"
 *   \brief Project-wide defintions and declarations.
 */

#ifndef _GLOBAL_H_
#define _GLOBAL_H_

#include <stdlib.h>
#include <stdio.h>
#include <string.h> 
#include "define.h"

// global variables------------------------------------------------
extern const AVS_FLOAT fPictureRates[];
extern const AVS_BYTE NCBP[64][2];
extern const AVS_BYTE NCBP422[4][2];
extern const AVS_DWORD MvNum[53];
extern const AVS_INT DequantTable[64];
extern const AVS_INT ShiftTable[64];
extern AVS_BYTE ClipTable[CLIPSIZE*2+256];
extern AVS_INT  TabNoBuf[1025];
extern AVS_INT  TabNoBuf2[1025];
extern AVS_SHORT * g_pCoffData;
extern AVS_SHORT * g_pInvCoffData;
// dingo-add
extern int Blockcount[3][3];
extern int GBT[13][13];
extern int GBT5[13][13];
extern int GBT8[13][13];
STREAMINFO strmInfo;
FILE *p_OUT;

/* TODO: Conditional definition for x86_64 */
#ifdef ARCH_X86_64
#  define LEGACY_REGS "=Q"
#else
#  define LEGACY_REGS "=q"
#endif

#if defined(__i386__) || defined(ARCH_X86) || defined(ARCH_X86_64)
static __inline__ AVS_UINT bswap_32(AVS_UINT x)
{
	__asm__ (
#if __CPU__ != 386
		"bswap   %0":
		"=r" (x)     :
#else
		"xchgb   %b0, %h0\n"
		"rorl    $16, %0\n"
		"xchgb   %b0, %h0":
		LEGACY_REGS (x)                :
#endif
	"0" (x));
	return x;
}
#endif

#define DWORD_SWAP(x) (( ((x) << 24) | ((x) >> 24) | (((x) & 0xFF00) << 8) | (((x) & 0xFF0000) >> 8)))

//ycb static __inline__ AVS_DWORD DWORD_SWAP (AVS_DWORD x)
// static AVS_DWORD DWORD_SWAP (AVS_DWORD x)
// {
// #if !(defined(__i386__) || defined(ARCH_X86) || defined(ARCH_X86_64))
// 	return ((AVS_DWORD)( ((x) << 24) | ((x) >> 24) |
// 	        (((x) & 0xFF00) << 8) | (((x) & 0xFF0000) >> 8)));
// 
// #else /* defined(__i386__) || defined(ARCH_X86) || defined(ARCH_X86_64) */
// 
// #ifdef ARCH_X86_64
// 	__asm__(
// 		"bswap  %0":
// 		"=r" (x)   :
// 		"0" (x));  
// 	return x; 
// #else
// 	union {
// 		uint64_t ll;
// 		struct { 
// 			uint32_t l,h;
// 		} l;
// 	} r;
// 	r.l.l = bswap_32(x);
// 	r.l.h = bswap_32(x >> 32);
// 	return r.ll;
// #endif
// 
// #endif /* defined(__i386__) || defined(ARCH_X86) || defined(ARCH_X86_64) */
// }
//ycb static __inline__ AVS_DWORD DWORD_SWAP (AVS_DWORD x)
// static AVS_DWORD DWORD_SWAP (AVS_DWORD x)
// {
// #if !(defined(__i386__) || defined(ARCH_X86) || defined(ARCH_X86_64))
// 	return ((AVS_DWORD)( ((x) << 24) | ((x) >> 24) |
// 	        (((x) & 0xFF00) << 8) | (((x) & 0xFF0000) >> 8)));
// 
// #else /* defined(__i386__) || defined(ARCH_X86) || defined(ARCH_X86_64) */
// 
// #ifdef ARCH_X86_64
// 	__asm__(
// 		"bswap  %0":
// 		"=r" (x)   :
// 		"0" (x));  
// 	return x; 
// #else
// 	union {
// 		uint64_t ll;
// 		struct { 
// 			uint32_t l,h;
// 		} l;
// 	} r;
// 	r.l.l = bswap_32(x);
// 	r.l.h = bswap_32(x >> 32);
// 	return r.ll;
// #endif
// 
// #endif /* defined(__i386__) || defined(ARCH_X86) || defined(ARCH_X86_64) */
// }

// global functions------------------------------------------------
void MakeClipTable();
void MakeTabNoBuf();
AVS_HRESULT GetMem(VIDEODATA** refFrame, AVS_INT refFrameNum);
void ReleaseMem(VIDEODATA* refFrame[], AVS_INT refNum);

#endif
